<?
layout("/inc/layout.html",{title:sysTitle}){
?>

<div id="page-wrapper">
      <!--BEGIN TITLE & BREADCRUMB PAGE-->
      <div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">
          <div class="page-header pull-left">
              <div class="page-title">分布式任务调度</div>
          </div>
          <ol class="breadcrumb page-breadcrumb pull-left">
              <li><i class="fa fa-home"></i>&nbsp;<a href="main.html">首页</a>&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
              <li>分布式任务调度&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
              <li class="active">调度管理</li>
          </ol>
           
          <div class="clearfix"></div>
      </div>
      <!--END TITLE & BREADCRUMB PAGE-->
      <!--BEGIN CONTENT-->
      <div class="page-content">
      <div class="row mbl">
      		<div class="col-lg-4">
				<div class="input-group">
              		<span class="input-group-addon">执行器</span>
             			<select class="form-control" id="jobGroup"  >
                		</select>
            	</div>
	        </div>
      		<div class="col-lg-4">
				<div class="input-group">
	                <span class="input-group-addon">JobHandler</span>
                    <input type="text" id="jobHandler"  placeholder="请输入“JobHandler”" class="form-control" />
	           		<span class="input-group-btn"><button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" onclick="list1()">&nbsp;&nbsp;搜索&nbsp;&nbsp; </button></span>
	            </div>
	        </div>
           <div class="col-lg-4">
           <div class="tb-group-actions pull-right"><span>当前共<label id="pageSize"></label>条记录:</span>
                   <select id="pageNumber" class="table-group-action-input form-control input-inline input-small input-sm mlm" onchange="list1()">
                       <option value="5">5</option>
                       <option value="10" >10</option>
                       <option value="15" selected>15</option>
                       <option value="25">25</option>
                       <option value="50">50</option>
                   </select>&nbsp;
               <div class="tb-group-actions pull-right">
                <button class="btn btn-sm btn-primary" onclick="add('0')">新增
                   </button>
                   </div>
               </div>
           </div>
       </div>
      <table class="table table-hover table-striped table-bordered table-advanced tablesorter mbn">
            <thead>
                <tr>                 
                    <th >序号</th>
                    <th >JobKey</th>
                    <th >描述</th>
                    <th >执行器</th>
                    <th >JobHandler</th>
                    <th >Cron</th>
                    <th >负责人</th>
                    <th >状态</th>
                    <th >可操作</th>
                </tr>
            </thead>
            <tbody id="newspaperls"></tbody>
        </table>
<div id="dataNull" class="panel-body"
				style="height: 90px; line-height: 50px;" align="center">
				<span class='subtitle' style="font-size: 15px;">数据为空!</span>
		</div>
        <div id="page1" style="text-align: center; margin-top: 20px;"></div>
    </div>
                     
    <!--END CONTENT-->
    </div>

  
<?} ?>
<script>
 var cur;var id;
$(function(){
    cur=1;
	init();
	list1(cur);
});
function get(){
  list1(cur);
}
function getlay() {  
    return id;
}
function list1(curr) {
	$.getJSON("./xxljob_pageList.do",
	{
		page : curr || 1,
		pageNumber:$("#pageNumber").val(),
		jobHandler:$("#jobHandler").val(),
		jobGroup:$("#jobGroup option:selected").val(),
	},
	function(res) {
	//此处仅仅是为了演示变化的内容
		var str = "";
		if(res.listsize>0){
			$("#dataNull").hide();
		}else{
			$("#dataNull").show();
		}
		$("#newspaperls").html("")
		for ( var i = 0; i < res.listsize; i++) {
			str += "<tr><td align=\"center\"> "
					+ (i + 1)
					+ "</td><td> <label >"
					+  res.result[i].xxlJobGroup.id+"_"+ res.result[i].id
					+ "</label></td><td><label >"
					+ res.result[i].id+">>"+res.result[i].jobDesc
					+ "</label></td><td><label >"
					+ res.result[i].xxlJobGroup.title
					+ "</label></td><td><label >"
					+ (res.result[i].glueSwitch > 0 ?"GLUE模式":res.result[i].executorHandler)
					+ "</label></td><td><label >"
					+ res.result[i].jobCron
					+ "</label></td><td><label >"
					+ res.result[i].author
					+ "</label></td><td>";
					if(res.result[i].jobStatus=="NORMAL"){
						str +=" <span class=\"label label-sm label-success\"> "
						+ res.result[i].jobStatus
						+ "</span></td><td>";
					}else if(res.result[i].jobStatus=="PAUSED"){
						str +=" <span class=\"label label-sm label-default\"> "
						+ res.result[i].jobStatus
						+ "</span></td><td>";
					}else if(res.result[i].jobStatus=="BLOCKED"){
						str +=" <span class=\"label label-sm label-default\"> "
						+ res.result[i].jobStatus
						+ "</span></td><td>";
					}
					
					str += "	<button type=\"button\" class=\"btn btn-blue btn-xs\" onclick=\"trigger("+res.result[i].id+")\">执行</button>";
					if('NORMAL' == res.result[i].jobStatus){
						str += "	<button type=\"button\" class=\"btn btn-blue btn-xs\" onclick=\"pause("+res.result[i].id+")\">暂停</button>";
					} else if ('PAUSED' == res.result[i].jobStatus){
						str += "	<button type=\"button\" class=\"btn btn-blue btn-xs\" onclick=\"resume("+res.result[i].id+")\">恢复</button>";
					}
					
					str+="	<button type=\"button\" class=\"btn btn-blue btn-xs\" onclick=\"logDetail("+res.result[i].id+")\">日志</button>"
					+"	<button type=\"button\" class=\"btn btn-yellow btn-xs\" onclick=\"add("+res.result[i].id+")\">编辑</button>";
					if(res.result[i].glueSwitch == 1){
						str	+= "	<button type=\"button\" class=\"btn btn-yellow btn-xs\" onclick=\"glue("+res.result[i].id+")\">GLUE</button>";
					}
					str	+= "	<button type=\"button\" class=\"btn btn-red btn-xs\" onclick=\"remove1("+res.result[i].id+")\">删除</button>"
					
					+ "</td></tr>";
		}
		$("#newspaperls").append(str);
        document.getElementById('pageSize').innerHTML=res.listsize;
		laypage({
			cont : document.getElementById('page1'), //容器。值支持id名、原生dom对象，jquery对象,
			pages : res.pageSize, //总页数
			skip : true, //是否开启跳页
			skin : 'molv', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
			curr : curr || 1,
			last: res.pageSize,
			groups : 6,//连续显示分页数
			jump : function(obj, first) { //触发分页后的回调
				if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
					list1(obj.curr);
					var curr = obj.curr;
					curr = obj.curr;
				}
			}
		});
	});
}
//添加编辑
function add(lid) {
	id=lid;
	layer.open({
		type : 2,
		area : [ '800px', '510px' ],
		scrollbar : false,
		shadeClose : true,
		fix : true, //不固定
		title : "新增任务",
		//maxmin : true,  
		skin : 'layui-layer-rim', //加上边框
		btn : [ "保存 ", "关闭" ],
		content : [ "xxl-job-info-add.html", "no" ],
		yes : function(index, layer) {
			var iframeWin = parent.window[layer.find("iframe")[0]["name"]];
			iframeWin.Save();
		},
		cancel : function(index) {
		}

	});
}
//日志跳转
function logDetail(lid) {
	id=lid;
	window.location.href="xxl-job-log-list.html?id="+id;
	//document.location.href("xxl_job_log_logdetail.html"); 
}
//删除			
function remove1(id) {
	layer.confirm("是否确认删除？", {
		  btn: ["确定","删除"] //按钮
		}, function(){
			$.ajax({
				type : "post",
				url : "./xxljob_remove.do",
				data : {
					"id" : id
				},
				dataType : "json",
				success : function(data) {
					if(data.result=="ok"){
						layer.msg("删除成功！", {icon: 6});
					}else{
						layer.msg("删除失败！", {icon: 5});
					}
					get();
				},
				error : function(data) {
					layer.msg("请求失败！", {icon: 2});
				}
			});
		}, function(){
	});
}
//恢复		
function resume(id) {
	layer.confirm("是否确认恢复？", {
		  btn: ["确定","取消"] //按钮
		}, function(){
			$.ajax({
				type : "post",
				url : "./xxljob_resume.do",
				data : {
					"id" : id
				},
				dataType : "json",
				success : function(data) {
					if(data.result=="ok"){
						layer.msg("恢复成功！", {icon: 6});
					}else{
						layer.msg("恢复失败！", {icon: 5});
					}
					get();
				},
				error : function(data) {
					layer.msg("请求失败！", {icon: 2});
				}
			});
		}, function(){
	});
}
//暂停		
function pause(id) {
	layer.confirm("是否确认暂停？", {
		  btn: ["确定","取消"] //按钮
		}, function(){
			$.ajax({
				type : "post",
				url : "./xxljob_pause.do",
				data : {
					"id" : id
				},
				dataType : "json",
				success : function(data) {
					if(data.result=="ok"){
						layer.msg("已暂停！", {icon: 6});
					}else{
						layer.msg("暂停失败！", {icon: 5});
					}
					get();
				},
				error : function(data) {
					layer.msg("请求失败！", {icon: 2});
				}
			});
		}, function(){
	});
}
//执行		
function trigger(id) {
	layer.confirm("是否确认执行？", {
		  btn: ["确定","取消"] //按钮
		}, function(){
			$.ajax({
				type : "post",
				url : "./xxljob_trigger.do",
				data : {
					"id" : id
				},
				dataType : "json",
				success : function(data) {
					if(data.result=="ok"){
						layer.msg("执行启动成功！", {icon: 6});
					}else{
						layer.msg("执行启动失败！", {icon: 5});
					}
					get();
				},
				error : function(data) {
					layer.msg("请求失败！", {icon: 2});
				}
			});
		}, function(){
	});
}
//glue编辑
function glue(lid){
	id=lid;
	layer.open({
		type : 2,
		area : [ '800px', '480px' ],
		scrollbar : false,
		shadeClose : true,
		fix : true, //不固定
		title : "WEBIDE			任务:"+id,
		//maxmin : true,  
		skin : 'layui-layer-rim', //加上边框
		btn : [ "保存 ", "关闭" ],
		content : [ "xxl-job-code-edit.html", "no" ],
		yes : function(index, layer) {
			var iframeWin = parent.window[layer.find("iframe")[0]["name"]];
			iframeWin.Save();
		},
		cancel : function(index) {
		}

	});
}
//获取执行器
function init(){
	$.ajax({
		type : "post",
		url : "./xxljob_log_init.do",
		data : {
			"id":"0",
		},
		dataType : "json",
		async : false,
		success : function(data) {
			var jobinfoHtml="";
			var jobgrouphtml="";
			//jobgrouphtml += "<option value=\"0\" >请选择</option>";
			for(var i=0;i<data.jobGroupsize;i++){
				if(i==0){
					jobgrouphtml += "<option value=\""+data.jobGroup[i].id+"\" selected=\"selected\">"+data.jobGroup[i].title+"</option>";
				}else{
					jobgrouphtml += "<option value=\""+data.jobGroup[i].id+"\">"+data.jobGroup[i].title+"</option>";
				}
			}
			
			$("#jobGroup").append(jobgrouphtml);
		},
		error : function(data) {
			layer.msg("请求失败！", {icon: 2});
		}
	});
}
</script>